/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import domain.Userdom;
import domain.Wcontract;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

/**
 *
 * @author Ale
 */
public class WcontractDao {

    EntityManager em;
    EntityTransaction tx;

    public WcontractDao() {
        em = SingletonDao.getInstance().getEntityManager();
        tx = SingletonDao.getTx();
    }

    public Wcontract getWcontract(Integer idWcontract) {
        Wcontract w = (Wcontract) em.createNativeQuery("select * from wcontract as n where n.idWcontract='" + idWcontract + "'", Wcontract.class).getSingleResult();
        return w;
    }

    public void saveWContract(Wcontract w) {
        tx.begin();
        em.persist(w);
        tx.commit();
    }

    public void removeWcontract(Wcontract w) {
        tx.begin();
        em.remove(w);
        tx.commit();
    }

    public List<Wcontract> getWContrList(Userdom user) {
        tx.begin();
        List list = em.createNativeQuery("select * from bankaccount b, wcontract w where b.userFK='"+user.getNick()+"' and w.bankFK=b.idBank", Wcontract.class).getResultList();
        tx.commit();
        return list;
    }

    public void updateWContract(Wcontract wc) {
        tx.begin();
        em.createNativeQuery("update wcontract set workName='"+wc.getWorkName()+"', basicSalary='"+wc.getBasicSalary()+"' where idWcontract='"+wc.getIdWcontract()+"'").executeUpdate();
        tx.commit();
    }

}
